package org.dfl.core.model.transitions;

import org.dfl.core.data.types.Type;
import org.dfl.core.data.types.TypeManager;
import org.dfl.core.model.TransitionExecutionException;
import org.dfl.messages.Messages;


/**
 * transition to convert input string to a boolean value
 * @author Piotr Wlodarczyk
 *
 */
public class ToBooleanTransition extends OneInputTransition {
	@Override
	public String getDefaultDescription() {
		return Messages.ToBooleanTransition_description;
	}

	@Override
	protected void connectInputAndOutput() {
		// nothing to do - fixed types
	}

	@Override
	protected Object fireSingle(Object input) throws TransitionExecutionException {
		String str = input.toString().toLowerCase();
		if(str == null || (!str.equals("true") && !str.equals("false"))){ //$NON-NLS-1$ //$NON-NLS-2$
			throw new TransitionExecutionException(Messages.ToBooleanTransition_errorBooleanStr+str);
		}
		
		return str.equals("true") ? Boolean.TRUE : Boolean.FALSE; //$NON-NLS-1$
	}
	
	@Override
	protected Type getDefaultInputDefinition() {
		return TypeManager.getAnyType();
	}
	@Override
	protected Type getDefaultOutputDefinition() {
		return TypeManager.getBooleanType();
	}
}
